package com.self.quartz;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * 作者：tianjiayuan
 * 创建时间：2018-06-06 19:02
 * 类描述：
 * 修改人：
 * 修改时间：
 */
public class QuartzTest {

    private ConcurrentMap<Long, Scheduler> schedulerBox = new ConcurrentHashMap<>();

    public void createSchedule(Long courseId, String timetable) {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            schedulerBox.putIfAbsent(courseId, scheduler);

            //创建一个job
            JobDetail job = newJob(HelloJob.class)
                    .withIdentity("job1", "group1")
                    .build();

            //创建一个触发器
            Trigger trigger = newTrigger()
                    .withIdentity("trigger1", "group1")
                    .startNow()
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(10)
                            .withRepeatCount(3))
                    .build();


            scheduler.scheduleJob(job, trigger);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void start(Long courseId) throws Exception{
        this.schedulerBox.get(courseId).start();
    }

    public void stop(Long courseId) throws Exception{
        this.schedulerBox.get(courseId).shutdown();
    }

    public static void main(String[] args) {

//        try {
//
//            QuartzTest qt = new QuartzTest();
//
//            qt.createSchedule(123456L, "");
//
//            qt.start(123456L);
//
//            while (!HelloJob.isFinish) {
//                ;
//            }
//
//            qt.stop(123456L);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        System.out.println("aa".hashCode());

    }
}
